package com.liwenfeng.dodizhu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Room {
    private final List<Card> allCards = new ArrayList<>();

    public Room() {
        String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        String[] colors = {"♠", "♥", "♣", "♦"};
        int grade = 0;
        for (String num : nums) {
            grade++;
            for (String color : colors) {
                allCards.add(new Card(num, color, grade));
            }
        }
        allCards.add(new Card("", "🐨", ++grade));
        allCards.add(new Card("", "🤡", ++grade));

    }

    public void start() {
        Collections.shuffle(allCards);
        List<Card> player1 = new ArrayList<>();
        List<Card> player2 = new ArrayList<>();
        List<Card> player3 = new ArrayList<>();
        for (int i = 0; i < allCards.size() - 3; i++) {
            if (i % 3 == 0) {
                player1.add(allCards.get(i));
            } else if (i % 3 == 1) {
                player2.add(allCards.get(i));
            } else {
                player3.add(allCards.get(i));
            }
        }
        List<Card> lastThree = new ArrayList<>(allCards.stream().skip(51).toList());
        sortCarts(player1, player2, player3);
        player2.addAll(lastThree);
        sortCarts(player2);
        System.out.println(player1);
        System.out.println(player2);
        System.out.println(player3);

    }

    @SafeVarargs
    private void sortCarts(List<Card>... players) {
        for (List<Card> player : players) {
            player.sort(((o1, o2) -> o1.getGrade() - o2.getGrade()));
        }
    }

}
